package solutions.leetcode.normal;

import solutions.lib.BaseSolution;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/nth-digit/">400. 第 N 位数字</a>
 */
public class Solution400 extends BaseSolution {

    public int findNthDigit(int n) {
        if (n < 10) {
            return n;
        }
        final int stepUnit = 9;
        int floor = 1;
        while (true) {
            long len = (long) ((long) stepUnit * Math.pow(10, floor - 1) * floor);
            if (n > len) {
                n -= len;
                floor++;
            } else {
                int target = (int) ((int) Math.ceil((float) n / (floor)) + Math.pow(10, floor - 1) - 1);
                String tStr = String.valueOf(target);
                int ind = (n - 1) % floor;
                return Integer.parseInt(String.valueOf(tStr.charAt(ind)));
            }
        }
    }

    public static void main(String[] args) {
        Solution400 solution = new Solution400();
        for (int i = 0; i < 1200; i++) {
            System.out.println(solution.findNthDigit(i));
        }
    }
}